﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GrainManage.Model;
using System.ComponentModel;

namespace GrainManage.Tools
{
    public class PageHelper
    {
        #region 构造函数
        private int totalRowNumber;
        private int pageSize;
        private int currentPageIndex;

        private readonly object theLock = new object();

        public PageHelper()
        {
            this.pageSize = 10;
        }

        public PageHelper(int pageSize)
        {
            if (pageSize > 0)
            {
                this.pageSize = pageSize;
            }
        }

        //public PageHelper(int totalRowNumber, int pageSize)
        //    : this(pageSize)
        //{
        //    if (totalRowNumber > 0)
        //    {
        //        this.totalRowNumber = totalRowNumber;
        //    }
        //}

        #endregion

        #region 公共属性

        public int MaxPageIndex { get; private set; }

        public int MaxPageNumber { get { return totalRowNumber == 0 ? 0 : MaxPageIndex + 1; } }

        public int CurrentPageIndex
        {
            get { return currentPageIndex; }
            set
            {
                lock (theLock)
                {
                    int current = value;
                    if (current >= 0)
                    {
                        currentPageIndex = current > MaxPageIndex ? MaxPageIndex : current;
                    }
                }
            }
        }

        public int CurrentPageNumber { get { return totalRowNumber == 0 ? 0 : currentPageIndex + 1; } }

        public int TotalRowNumber
        {
            get { return totalRowNumber; }
            set
            {
                lock (theLock)
                {
                    int newTotal = value;
                    if (newTotal >= 0 && newTotal != totalRowNumber)
                    {
                        MaxPageIndex = newTotal == 0 ? 0 : (newTotal - 1) / PageSize;
                        if (currentPageIndex > MaxPageIndex) { currentPageIndex = MaxPageIndex; }
                        totalRowNumber = newTotal;
                    }
                }
            }
        }

        public int PageSize
        {
            get { return pageSize; }
            set
            {
                lock (theLock)
                {
                    int newPageSize = value;
                    if (newPageSize > 0 && newPageSize != pageSize)
                    {
                        MaxPageIndex = totalRowNumber == 0 ? 0 : (totalRowNumber - 1) / newPageSize;
                        currentPageIndex = currentPageIndex * pageSize / newPageSize;
                        pageSize = newPageSize;
                    }
                }
            }
        }

        public int NextPageIndex
        {
            get
            {
                if (currentPageIndex < MaxPageIndex)
                {
                    lock (theLock)
                    {
                        ++currentPageIndex;
                    }
                }
                return currentPageIndex;
            }
        }

        public int PreviousPageIndex
        {
            get
            {
                if (currentPageIndex > 0)
                {
                    lock (theLock)
                    {
                        --currentPageIndex;
                    }
                }
                return currentPageIndex;
            }
        }

        #endregion
    }
}
